# -*- coding: utf-8 -*-
import scrapy


class YgSpider(scrapy.Spider):
    name = 'yg'
    allowed_domains = ['wz.sun0769.com']
    start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4&page=0']

    def parse(self, response):
        tr_list = response.xpath('//div[@class="greyframe"]/table[2]/tr/td/table/tr')
        print(len(tr_list))
        for tr in tr_list:
            item = {}
            item['编号'] = tr.xpath('./td[1]/text()').extract_first()
            item['标题'] = tr.xpath('./td[2]/a[2]/@title').extract_first()
            item['状态'] = tr.xpath('./td[3]/span/text()').extract_first()
            item['详情页'] = tr.xpath('./td[2]/a[2]/@href').extract_first()
            item['网友'] = tr.xpath('./td[4]/text()').extract_first()
            item['时间'] = tr.xpath('./td[5]/text()').extract_first()
            yield scrapy.Request(
                item['详情页'],
                callback=self.parse2,
                meta={'item': item}
            )

        next_url = response.xpath('//div[@class="pagination"]/a[text()=">"]/@href').extract_first()
        print(next_url)
        if next_url != None:
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )

    def parse2(self, response):
        item = response.meta['item']
        print(item['详情页'])
        item["content"] = response.xpath('//td[@class="txt16_3"]//text()').extract()
        yield item
